package homework;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Work04 {

    //1、声明一个Message类，包含：发送者、接收者、消息内容、发送时间
    //
    //	2、创建一个Message对象，并写到message.dat文件中，并再次读取显示
    public static void main(String[] args) throws IOException {

        File file = new File("day09-code/src/homework/message.dat");
        //创建一个Message对象
        Message message = new Message(
                "张三", "小美", "今天天气不错", "2020-09-09"
        );
        writeFile(file, message);
        readFile( file);
        // 读取并重建对象
        Message readMessage = readFile1(file); // 注意：这里调用的是能返回对象的方法
        if (readMessage != null) {
            System.out.println("\n 从文件读取的消息：");
            System.out.println("发送者：" + readMessage.getSender());
            System.out.println("接收者：" + readMessage.getReceiver());
            System.out.println("内容：" + readMessage.getContent());
            System.out.println("时间：" + readMessage.getTime());
        } else {
            System.out.println("❌ 读取失败，文件为空或格式错误！");
        }
    }

    //创建一个方法，将Message对象写入到message.dat文件中
    public static void writeFile(File file, Message message){
        //创建输出流写到message.dat文件中
        try(FileWriter fr = new FileWriter(file);//创建字符输入流
            BufferedWriter bw = new BufferedWriter(fr)){//创建缓冲流

            //定义一个字符串，输入到文件中
            String line = message.getSender()
                    + "--" + message.getReceiver()
                    + "--" + message.getContent()
                    + "--" + message.getTime();

            bw.write(line);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //创建一个方法，从message.dat文件中读取数据并显示
    public static void readFile(File file) {
        // 基本流：FileInputStream（字节输入流）
        try (FileReader fis = new FileReader(file)) {
            int len;
            while ((len = fis.read()) != -1){
                System.out.print((char)len);
            }

            // 读取文件内容
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static Message readFile1(File file){
        // 基本流：FileInputStream（字节输入流）
        try (FileInputStream fis = new FileInputStream(file);//创建字节输入流
             // 转换流：InputStreamReader（将字节流转换为字符流，指定编码UTF-8）
             InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
             // 包装流：BufferedReader（包装字符流，提供缓冲读取）
             BufferedReader br = new BufferedReader(isr)) {

            // 读取文件内容
            String line = br.readLine();
            if (line != null) {
                // 按分隔符拆分字符串
                String[] parts = line.split("--");

                // 解析各属性（ sender | receiver | content | sendTime ）
                String sender = parts[0];
                String receiver = parts[1];
                String content = parts[2];
                String sendTime = parts[3];  // 将字符串转换为Date

                // 返回解析后的Message对象
                return new Message(sender, receiver, content, sendTime);
            }

        } catch (IOException  e) {
            e.printStackTrace();
        }
        return null;
    }

}

class Message {

    private String sender;
    private String receiver;
    private String content;
    private String time;
    public Message() {
    }

    public Message(String sender, String receiver, String content, String time) {
        this.sender = sender;
        this.receiver = receiver;
        this.content = content;
        this.time = time;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}